package notice

import (
	"fmt"
	"github.com/gin-gonic/gin"
	dto "vrcm/src/vrcm/app/admin/service/dto/notice"
	service "vrcm/src/vrcm/app/admin/service/notice"
	"vrcm/src/vrcm/common/apis"
)

type Notice struct {
	apis.Api
}

func (e Notice) Insert(c *gin.Context) {
	req := dto.NoticeInsertReq{}
	s := service.Notice{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Insert(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("创建公告失败， %s", err.Error()))
		return
	}

	e.OK(req.Id, "创建成功")
}

func (e Notice) Delete(c *gin.Context) {
	req := dto.NoticeDeleteReq{}
	s := service.Notice{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.Delete(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除公告失败， %s", err.Error()))
		return
	}
	e.OK(nil, "删除成功")
}

func (e Notice) GetNotice(c *gin.Context) {
	req := dto.NoticeGetReq{}
	s := service.Notice{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetNotice(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取公告失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取公告成功")
}
